
//
// 130228 - AH - Created.
// 130504a- AH - Replaced Collection with List as type request methods returns.
// 130504b- AH - getItemWithProperty(String, Object, String).
// 130505 - AH - Implemented missing createRelation methods that accepts Item.
// 130517 - AH - getRelations(Item, String).
//

package org.japril.gim;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *   Base class for 
 *   {@link Items} implementation. 
 * </p>
 * <p>
 *   Implements methods that can be implemented in terms of other methods.
 * </p>
 * @author Arne Halvorsen (AH)
 */
public abstract class AbstractItems implements Items
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  protected AbstractItems(){}

  // Specified in Items.
  public final Item create() throws IOException { return create(null); }

  // Specified in Items.  
  public final boolean delete(Item item) throws IOException
  {
    if (item==null)
    {
      throw new NullPointerException("l");
    }
    
    return delete(item.getId());
  }
 
  // Specified in Items.
  public final List<Item> getItems() throws IOException 
  { 
    return getItems(null); 
  }

  // Specified in Items.
  public Item getItemWithProperty(String name, Object val, String type)
    throws IOException
  {
    List<Item> items=getItemsWithProperty(name, val, type);
    return items.isEmpty() ? null : items.get(0);
  }

  // Specified in Items.
  public void createRelation(Item li, Item ri) throws IOException
  {
    createRelation(li.getId(), ri.getId());
  }

  // Specified in Items.
  public void createRelation(Item li, Item ri, String rrole) throws IOException 
  {
    createRelation(li.getId(), ri.getId(), rrole);
  }

  // Specified in Items.
  public void createRelation(Item li, String lrole, Item ri) throws IOException
  {
    createRelation(li.getId(), lrole, ri.getId());
  }

  // Specified in Items.
  public void createRelation(Item li, String lrole, Item ri, String rrole)
    throws IOException
  {
    createRelation(li.getId(), lrole, ri.getId(), rrole);
  }
  
  // Specified in Items.
  public final void createRelation(int lid, int rid) throws IOException
  {
    createRelation(lid, null, rid, null);
  }

  // Specified in Items.
  public final void createRelation(int lid, int rid, String rrole) 
    throws IOException 
  {
    createRelation(lid, null, rid, rrole);
  }

  // Specified in Items.
  public final void createRelation(int lid, String lrole, int rid) 
    throws IOException
  {
    createRelation(lid, lrole, rid, null);
  }
  
  // Specified in Items.
  public final boolean deleteRelation(int lid, int rid) throws IOException
  {
    return deleteRelation(lid, null, rid, null);
  }

  // Specified in Items.
  public final boolean deleteRelation(int lid, int rid, String rrole) 
    throws IOException 
  {
    return deleteRelation(lid, null, rid, rrole);
  }

  // Specified in Items.
  public final boolean deleteRelation(int lid, String lrole, int rid) 
    throws IOException
  {
    return deleteRelation(lid, lrole, rid, null);
  }
   
  // Specified in Items.
  public List<ItemItem> getRelations(Item li, String rtype) throws IOException
  {
    if (li==null)
    {
      throw new NullPointerException("li");  
    }
    
    return getRelations(li.getId(), rtype);
  }

  // Specified in Items.
  public final Item getRightItem(int lid) throws IOException
  {
    return getRightItem(lid, null);
  }

  // Specified in Items.
  public final List<Item> getRightItems(int lid) throws IOException
  {
    return getRightItems(lid, null);
  }
  
  // Specified in Items.
  public final Item getLeftItem(int rid) throws IOException
  {
    return getLeftItem(rid, null);
  }

  // Specified in Items.
  public final List<Item> getLeftItems(int rid) throws IOException
  {
    return getLeftItems(rid, null);
  }

  // Specified in Items.
  public final Item getRightItem(Item l) throws IOException
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    return getRightItem(l.getId());
  }

  // Specified in Items.
  public final Item getRightItem(Item l, String rrole) throws IOException
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    return getRightItem(l.getId(), rrole);
  }

  // Specified in Items.
  public final List<Item> getRightItems(Item l) throws IOException
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    return getRightItems(l.getId());
  }

  // Specified in Items.
  public final List<Item> getRightItems(Item l, String rrole)
    throws IOException 
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    return getRightItems(l.getId(), rrole);
  }

  // Specified in Items.
  public final Item getLeftItem(Item r) throws IOException
  {
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return getLeftItem(r.getId());
  }

  // Specified in Items.
  public final Item getLeftItem(Item r, String lrole) throws IOException 
  {
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return getLeftItem(r.getId(), lrole);
  }

  // Specified in Items.
  public final List<Item> getLeftItems(Item r) throws IOException
  {
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return getLeftItems(r.getId());
  }

  // Specified in Items.
  public final List<Item> getLeftItems(Item r, String lrole)
    throws IOException 
  {
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return getLeftItems(r.getId(), lrole);
  }

  // Specified in Items.
  public final boolean deleteRelation(Item l, Item r) throws IOException
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return deleteRelation(l.getId(), r.getId());
  }

  // Specified in Items.
  public final boolean deleteRelation(Item l, Item r, String rrole)
    throws IOException
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return deleteRelation(l.getId(), r.getId(), rrole);
  }

  // Specified in Items.
  public final boolean deleteRelation(Item l, String lrole, Item r)
    throws IOException
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return deleteRelation(l.getId(), lrole, r.getId());
  }

  // Specified in Items.
  public final boolean deleteRelation(Item l, String lrole, Item r, 
    String rrole) throws IOException 
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    if (r==null)
    {
      throw new NullPointerException("r");
    }
    
    return deleteRelation(l.getId(), lrole, r.getId(), rrole);
  }

  // Specified in Items.
  public void setProperty(Item item, String name, Object value)
    throws IOException 
  {
    if (item==null)
    {
      throw new NullPointerException("item");
    }
    
    setProperty(item.getId(), name, value); 
  }

  // Specified in Items.
  public String getStringProperty(Item item, String name) throws IOException
  {
    if (item==null)
    {
      throw new NullPointerException("item");
    }
    
    return getStringProperty(item.getId(), name);
  }

  // Specified in Items.
  public Integer getIntProperty(Item item, String name) throws IOException
  {
    if (item==null)
    {
      throw new NullPointerException("item");
    }
    
    return getIntProperty(item.getId(), name);
  }
  
}
